/*
import { mainStore } from '@/stores/mainStore'
let piniaMainStore = mainStore()

// 读取state值
console.log(piniaMainStore.name); //xxx

// 修改state值
piniaMainStore.name = 'hello world'
console.log(piniaMainStore.name);

// 批量修改，使用$patch方法可以修改多个state中的值,比如我们在piniaStore/storeA.js中的state增加一个name
// 也是支持修改单个状态
piniaMainStore.$patch({
    name: 'hello world2',
    count: 2,
})

// or 使用函数的方式进行修改

piniaMainStore.$patch((state) => {
  state.name = 'hello world2'
  state.count = 2
})

// 调用action
piniaMainStore.exit()

// 重置state，将状态重置为初始值
piniaMainStore.$reset()

 */
import { defineStore } from "pinia"
import axios from 'axios'
import tool from '@/utils/tool'
import sysConfig from "@/config"
import api from '@/api'
import cloneDeep from 'lodash.clonedeep'
import { filterMenusByMenuName, processLanguage, convertRoutes, filterAsyncRouter, flatAsyncRoutes } from '@/router/filterRouter'

import userRouter from '@/router/userRouter'
import config from '@/config' // mock的路由

//这里是对pinia进行一个取名，state需要时一个函数的形式
export const mainStore = defineStore("mainStore", {
// export default defineStore('mainStore', {
    state: () => {
        return {
            lang: '',
            count: 10,
            name: 'xxx',
            chooseCartIds: [],
            global: {
                dict: null
            }
        }
    },
    getters: {},
    // 同步和异步皆可
    actions: {
        async getUserMenus() {
            return new Promise(resolve => {
                // 因为现在是本地菜单，从mock数据里调
                // api.common.getRoutes.post().then( res => {
                //     resolve(res)
                // })
                // console.log('post 当前语言', this.lang)
                resolve(userRouter)
            })
        },
        /**
         * 重置路由的语言包
         */
        resetRoutesLanguage(userMenus) {
            let userMenusRouter = cloneDeep(userMenus)
            userMenusRouter = processLanguage(userMenusRouter)
            // 可以把用户路由存在缓存里
            tool.data.set(sysConfig.ROUTE_INFO_KEY, userMenusRouter)
            // 路由加面包屑
            userMenusRouter = flatAsyncRoutes(userMenusRouter)
            return userMenusRouter
        },
        generateRoutes(data) {
            return new Promise(resolve => {
                const { user, userMenus, allowMenus } = data
                let userMenusRouter = cloneDeep(userMenus)
                userMenusRouter = filterMenusByMenuName(userMenusRouter, allowMenus)
                // console.log('最后出来的路由', userMenusRouter)
                // 多加了一步处理title的语言包，如果不是多语言可以注释
                userMenusRouter = processLanguage(userMenusRouter)
                // console.log('userMenusRouter', userMenusRouter)
                // 可以把用户路由存在缓存里
                tool.data.set(sysConfig.ROUTE_INFO_KEY, userMenusRouter)
                // 根据返回的菜单，筛选出符合权限的菜单
                // 如果这一步后台已经筛选好了，则可以不用筛选
                userMenusRouter = filterAsyncRouter(userMenusRouter, user)
                // 路由加面包屑
                userMenusRouter = flatAsyncRoutes(userMenusRouter)
                // console.log(userMenusRouter)
                // 转换路由路由，由string类型的component加载上
                userMenusRouter = convertRoutes(userMenusRouter)
                // tool.data.set(sysConfig.ROUTE_INFO_KEY, userMenusRouter)
                resolve(userMenusRouter)
            })
        }
    }
})
